package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.soms.entity.BorrowBookLog;
import com.dingreading.cloud.soms.entity.table.BorrowBookLogTableDef;
import com.dingreading.cloud.soms.mapper.BorrowBookLogMapper;
import com.dingreading.cloud.soms.service.BorrowBookLogService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class BorrowBookLogServiceImpl extends BaseServiceImpl<BorrowBookLogMapper, BorrowBookLog, BorrowBookLogTableDef> implements BorrowBookLogService {

    @Override
    protected BorrowBookLogTableDef getTable() {
        return BorrowBookLogTableDef.borrowBookLog;
    }

    @Override
    public List<BorrowBookLog> getByBorrowBookId(String storeUid, Long borrowBookId) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.borrowBookId.eq(borrowBookId));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public void deleteByBorrowBookIdAndIdsNot(String storeUid, Long borrowBookId, List<Long> ids) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.borrowBookId.eq(borrowBookId));
        if (CollectionUtils.isNotEmpty(ids))
            condition.and(table.id.notIn(ids));

        getMapper().deleteByCondition(condition);
    }

    @Override
    public int updateReturnStatus(String storeUid, Long borrowBookId, Long id, String staffUid, String staffName, Date time) {

        BorrowBookLog data = UpdateEntity.of(BorrowBookLog.class);

        UpdateWrapper<BorrowBookLog> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.status, 2)
                .set(table.returnTime, time)
                .set(table.returnStaffUid, staffUid)
                .set(table.returnStaffName, staffName);

        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.borrowBookId.eq(borrowBookId))
                .and(table.id.eq(id))
                .and(table.status.eq(1));

        return getMapper().updateByCondition(data, condition);

    }

    @Override
    public Map<Long, Long> getByStoreBookIdBorrowCnt(String storeUid, List<Long> storeBookIdList) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.status.eq(1))
                .and(table.storeBookId.in(storeBookIdList));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeBookId, QueryMethods.count(table.id).as("cnt"))
                .from(table)
                .where(condition)
                .groupBy(table.storeBookId);

        List<Row> rows = getMapper().selectRowsByQuery(wrapper);

        Map<Long, Long> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(rows)) {
            for (Row row : rows) {
                Long storeBookId = row.getLong("store_book_id");
                Long cnt = row.getLong("cnt");
                map.put(storeBookId, cnt);
            }
        }
        return map;
    }

    @Override
    public List<BorrowBookLog> getByStoreUidAndBorrowIds(String storeUid, List<Long> borrowIds) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.borrowBookId.in(borrowIds));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public Map<Long, Long> deleteBorrowBookCnt(String storeUid, Long borrowBookId, List<Long> logIds) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.borrowBookId.eq(borrowBookId));
        if (CollectionUtils.isNotEmpty(logIds)) {
            condition.and(table.id.notIn(logIds));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeBookId, QueryMethods.count(table.id).as("cnt"))
                .from(table)
                .where(condition)
                .groupBy(table.storeBookId);

        List<Row> rows = getMapper().selectRowsByQuery(wrapper);

        Map<Long, Long> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(rows)) {
            for (Row row : rows) {
                Long storeBookId = row.getLong("store_book_id");
                Long cnt = row.getLong("cnt");
                map.put(storeBookId, cnt);
            }
        }
        return map;
    }
}
